package com.test.account.controller;

import com.test.account.dto.AccountResult;
import com.test.account.dto.Page;
import com.test.account.pojo.Account;
import com.test.account.service.AccountService;
import com.test.account.util.DataTable;
import com.test.account.util.utils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 57695 on 2017/3/11.
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private DataTable dateTable;

    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return "AccountList";
    }
    @RequestMapping(value = "/data",method = RequestMethod.GET)
    @ResponseBody
    public Page<Account> data(HttpServletRequest request){
//        System.out.println("llll");
        int pagesize=10;
        int startRecord=0;
        String[] columnsName = {"ac_code","ac_name","ac_remark","ac_state","ac_createtime","ac_logintime","ac_type"};
        String size = request.getParameter("length");
        if(!"".equals(size)&&size!=null){
            pagesize=Integer.parseInt(size);
        }
        String currentRecord = request.getParameter("start");
        if(!"".equals(currentRecord)&&currentRecord!=null){
            startRecord=Integer.parseInt(currentRecord);
        }
        System.out.println("length: " + size);
        System.out.println("start: " + currentRecord);

        String sortOrder = request.getParameter("order[0][column]");
        String sortDir = request.getParameter("order[0][dir]");
        System.out.println("sortOrder: " + sortOrder);
        System.out.println("sortDir: " + sortDir);

        String searchValue = request.getParameter("search[value]");
        int Count = dateTable.getRecord(searchValue);

        List<Account> result = dateTable.loadAccountList(pagesize,startRecord,columnsName[Integer.parseInt(sortOrder==null?"0":sortOrder)],sortDir,searchValue);
        Page<Account> page = new Page<>();
        page.setDraw(Integer.parseInt(request.getParameter("draw") == null ? "0" : request.getParameter("draw")) + 1);
        page.setRecordsTotal(Count);
        page.setRecordsFiltered(Count);
        page.setData(result);
        System.out.println(page);
        return page;
    }
    @RequestMapping(value = "/{code}",method = RequestMethod.GET)
    @ResponseBody
    public AccountResult delete(@PathVariable("code") long code){
        int result = accountService.deleteAccount(code);
        if(result>0){
            return new AccountResult(true,"delete success!");
        }else{
            return new AccountResult(false,"delete failure");
        }
    }

    @RequestMapping(value = "/{code}",method = RequestMethod.POST)
    @ResponseBody
    public AccountResult sealAndUnseal(@PathVariable("code") long code){
       Account a = accountService.getAccountByCode(code);
        if(a.getAc_state() == 3){
            a.setAc_state(1);
            int rel = accountService.updateAccount(a,code);
            if(rel>0){
                return new AccountResult(true,"unseal success!");
            }else{
                return new AccountResult(false,"unseal failure");
            }
        }else {
            a.setAc_state(3);
            int rel = accountService.updateAccount(a,code);
            if(rel>0){
                return new AccountResult(true,"seal success!");
            }else{
                return new AccountResult(false,"seal failure");
            }
        }
    }


    @RequestMapping(value = "/operate/{code}",method = RequestMethod.GET)
    public String edit(@PathVariable("code") long code,Model model){
        if(code!=0) {
            Account account = accountService.getAccountByCode(code);
            if (account != null) {
                model.addAttribute("account", account);
                return "AccountEdit";
            } else {
                return "AccountAdd";
            }
        }else{
            return "AccountAdd";
        }
    }
    @RequestMapping(value = "/update/{code}",method = RequestMethod.POST)
    public String update(@PathVariable long code,Account account,Model model){
        System.out.println("accountUpdate:"+account);
        if(code!=0&&account!=null){
            int re = accountService.updateAccount(account,code);
            if(re>0){
                return "redirect:/account/list";
            }else {
                model.addAttribute("status",500);
                model.addAttribute("error","Update account failure");
                return "error";
            }
        }
        return "redirect:/account/list";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Account account,Model model){
        System.out.println(account);
        if(account!=null){
            int re = accountService.addAccount(account);
            if(re>0){
                return "redirect:/account/list";
            }else{
                model.addAttribute("status",500);
                model.addAttribute("error","Add account failure");
                return "error";
            }
        }
        return "redirect:/account/list";
    }
}
